package ThreadDemo.ThreadDemo2;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:周次煜
 * Date: 2024-07-16
 * Time：23:03
 */

public class StateDemo1 {

    /**
     *
     * Java的线程的状态表示
     * 1， NEW : 未创建线程的状态
     * 2. RUNNABLE : 线程正在运行中
     * 3.
     * @param args
     */


    public static void main(String[] args) throws InterruptedException {


        Thread thread = new Thread(()-> {

        });

        System.out.println(thread.getState());   // NEW :  未创建线程的状态

        thread.start();


        System.out.println(thread.getState());  // RUNNABLE : 线程代表正在运行中



    }
}



class  StateDemo2 {
    /**
     * 状态：线程状态
     *  TIMED_WAITING : 超时间的线程阻塞
     * @param args
     */

    public static void main(String[] args) throws InterruptedException {
        Thread mainThread = Thread.currentThread();

        Thread thread1 = new Thread(()->{

            while(true) {
                System.out.println("main 正在运行...");
                try {
                    System.out.println(mainThread.getState());
                    Thread.sleep(1000);

                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }


            }

        });


        thread1.start();

    }



}


class  StateDemo3 {
    /**
     * 状态：线程状态
     *  TIMED_WAITING ： 阻塞状态： 有超时间的阻塞
     *  带参数的join 的线程等待
     * @param args
     */

    public static void main(String[] args) throws InterruptedException {
        Thread mainThread= Thread.currentThread();
        Thread thread1 = new Thread(()->{
            for (int i = 0; i <4 ; i++) {
                System.out.println("thread1");

            }
            System.out.println(mainThread.getState());  //WAITING
        });


        thread1.start();


        thread1.join(4000);





    }



}


class  StateDemo4 {
    /**
     * 状态：线程状态
     *
     *  WAITING ： 阻塞状态： 没有超时间的阻塞
     *  不带参数的线程等待
     *
     * @param args
     */

    public static void main(String[] args) throws InterruptedException {
        Thread mainThread= Thread.currentThread();
        Thread thread1 = new Thread(()->{
            for (int i = 0; i < 4; i++) {
                System.out.println("thread1");

            }

            System.out.println(mainThread.getState());


        });


        thread1.start();


        thread1.join();



    }



}
